package com.jsz.peini.controller.app;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.activity.ActivityInfo;
import com.jsz.peini.bean.order.GoldHistory;
import com.jsz.peini.bean.order.GoldHistoryExample;
import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.order.OrderInfoExample;
import com.jsz.peini.bean.order.OrderInfoExample.Criteria;
import com.jsz.peini.bean.order.OrderSeller;
import com.jsz.peini.bean.order.OrderSellerExample;
import com.jsz.peini.bean.seller.SellerInfo;
import com.jsz.peini.bean.task.TaskInfo;
import com.jsz.peini.bean.user.UserAccount;
import com.jsz.peini.bean.user.UserAccountExample;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.activity.ActivityInfoMapper;
import com.jsz.peini.mapper.coupon.CouponHistoryMapper;
import com.jsz.peini.mapper.coupon.CouponInfoMapper;
import com.jsz.peini.mapper.coupon.CouponUserMapper;
import com.jsz.peini.mapper.order.GoldHistoryMapper;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.order.OrderSellerMapper;
import com.jsz.peini.mapper.seller.SellerDiscountMapper;
import com.jsz.peini.mapper.seller.SellerInfoMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;
import com.jsz.peini.mapper.user.UserAccountMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.redis.util.UserRedisUtil;
import com.jsz.peini.service.order.OrderInfoService;
import com.jsz.peini.service.user.UserInfoService;

@Controller
public class OrderInfoContorller {
	private static final Logger logger = LoggerFactory.getLogger(OrderInfoContorller.class);
	@Autowired
	private OrderInfoService orderInfoService;
	@Resource
	private TaskInfoMapper taskInfoMapper;
	@Resource
	private CouponUserMapper couponUserMapper;
	@Resource
	private CouponInfoMapper couponInfoMapper;
	@Resource
	private OrderInfoMapper orderInfoMapper;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private UserInfoService userInfoService;
	@Resource
	private SellerInfoMapper sellerInfoMapper;
	@Resource
	private SellerDiscountMapper sellerDiscountMapper;
	@Resource
	private CouponHistoryMapper couponHistoryMapper;
	@Resource
	private OrderSellerMapper orderSellerMapper;
	@Resource
	private UserAccountMapper userAccountMapper;
	
	@Resource
	private GoldHistoryMapper goldHistoryMapper;
	
	@Resource
	private ActivityInfoMapper activityInfoMapper;
	
	
	

	
	/**
	 * 提交订单接口
	 * @author Han Guohong
	 * @param request
	 * @param userId
	 * @param taskId
	 * @param sellerInfoId
	 * @param totalMoney
	 * @param exclusiveMoney
	 * @param payType
	 * @param couponId
	 * @return
	 */
	@RequestMapping(value="/saveTaskOrder")
	@ResponseBody
	public Object saveTaskAndSellerOrder(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="taskId",required=false,defaultValue="0") Integer taskId,
			@RequestParam(value="sellerInfoId") Integer sellerInfoId,
			@RequestParam(value="totalMoney") Double totalMoney,
			@RequestParam(value="exclusiveMoney",required=false,defaultValue="0") Double exclusiveMoney,
			@RequestParam(value="payType") Integer payType,//1金币；2微信；3支付宝
			@RequestParam(value="couponId",required=false) Integer couponId,
			@RequestParam(value="rangId",required=false) Integer rangId
			){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try{
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			if(totalMoney == 0d){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "请输入正确的消费金额");
				return infoMap;
			}
			
			if(exclusiveMoney > totalMoney){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "总消费金额不允许小于不参与折扣金额");
				return infoMap;
			}
			
			//验证支付密码是否正确
			UserAccountExample accexample = new UserAccountExample();
			accexample.createCriteria().andUserIdEqualTo(userId);
			List<UserAccount> accounts = userAccountMapper.selectByExample(accexample);
			if(accounts.size() == 0){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			UserAccount userAccount = accounts.get(0);
			if(payType == 1 && StringUtils.isBlank(userAccount.getPayPassword())){
				infoMap.put("resultCode", Constants.NOPAYPASS);
				infoMap.put("resultDesc", "您还没有设置支付密码");
				return infoMap;
			}
			
			if(taskId != null && taskId != 0){
				infoMap = orderInfoService.saveTaskOrder(request, userId, taskId, sellerInfoId, totalMoney, exclusiveMoney, payType, couponId, rangId);
			}else{
				infoMap = orderInfoService.saveSellerOrder(request, userId, sellerInfoId, totalMoney, exclusiveMoney, payType, couponId, rangId);
			}
		}catch(Exception e){
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 我的账单明细
	 * @author Han Guohong
	 * @param request
	 * @param orderStatus
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/getMyOrderList")
	@ResponseBody
	public Object getMyOrderList(HttpServletRequest request,
			@RequestParam(value="userId") String userToken,
			@RequestParam(value="orderType",required=false) Integer orderType,//1任务买单；3到店买单；
			@RequestParam(value="status",required=false) Integer status,//-1已取消；2已完成；0待付款 1待评价
//			@RequestParam(value="stream",required=false) Integer stream,//1收入；0支出
			@RequestParam(value="payType",required=false) Integer payType,//1金币；2微信；3支付宝
			int pageNow,int pageSize){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try{
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			
			List<OrderInfo> orderList = new ArrayList<OrderInfo>();
			
			OrderInfoExample example = new OrderInfoExample();
			Page<?> page = PageHelper.startPage(pageNow,pageSize);
			if(orderType == null || orderType == 0){
				Criteria criteria1 = example.createCriteria();
				List<Integer> types = new ArrayList<Integer>();
				types.add(1);
				types.add(3);
				criteria1.andOrderTypeIn(types);
				if(payType != null && payType != 0){
					criteria1.andPayTypeEqualTo(payType);
				}
				/*if(payType != null && payType != 0){
					criteria1.andPayTypeEqualTo(payType);
				}*/
				
				if(status != null){
					criteria1.andOrderStatusEqualTo(status);
				}else{
					criteria1.andOrderStatusNotEqualTo(0);
					criteria1.andOrderStatusNotEqualTo(-1);
				}
//				if(stream != null && stream == 1){//收入
//					criteria1.andOrderTypeEqualTo(100);//故意增加额外条件不符合
//				}
				criteria1.andUserIdEqualTo(userId);
				
				/*Criteria criteria2 = example.createCriteria();
				Criteria criteria3 = example.createCriteria();
				types = new ArrayList<Integer>();
				types.add(4);
				types.add(5);
				criteria2.andOrderTypeIn(types);
				criteria3.andOrderTypeIn(types);
				if(payType != null && payType != 0){
					criteria2.andPayTypeEqualTo(payType);
					criteria3.andPayTypeEqualTo(payType);
				}
				if(payType != null && payType != 0){
					criteria2.andPayTypeEqualTo(payType);
					criteria3.andPayTypeEqualTo(payType);
				}				
				if(status != null && status != 0){
					criteria2.andOrderStatusEqualTo(status);
					criteria3.andOrderStatusEqualTo(status);
				}*/
//				if(stream != null && stream == 1){//收入
//					criteria2.andOtherIdEqualTo(userId);
//				}else if(stream != null && stream == 0){
//					criteria2.andUserIdEqualTo(userId);
//				}else{
//					criteria3.andUserIdEqualTo(userId);
//					criteria2.andOtherIdEqualTo(userId);
//					example.or(criteria3);
//				}
				
				/*example.or(criteria2);
				example.or(criteria3);*/
				example.setOrderByClause("order_time desc");
				orderList = orderInfoMapper.selectByExample(example);
			}else{
				Criteria criteria = example.createCriteria();
				//Criteria criteria2 = example.createCriteria();
				
				criteria.andOrderTypeEqualTo(orderType);
				//criteria2.andOrderTypeEqualTo(orderType);
				
				if(payType != null && payType != 0){
					criteria.andPayTypeEqualTo(payType);
					//criteria2.andPayTypeEqualTo(payType);
				}
				
				if(status != null){
					criteria.andOrderStatusEqualTo(status);
					//criteria2.andOrderStatusEqualTo(status);
				}else{
					criteria.andOrderStatusNotEqualTo(0);
					criteria.andOrderStatusNotEqualTo(-1);
				}
				criteria.andUserIdEqualTo(userId);
				
//				if(orderType == 1 || orderType == 3){
//					if(stream != null && stream == 1){//收入
//						criteria.andOrderTypeEqualTo(100);//故意增加额外条件不符合
//					}
//					criteria.andUserIdEqualTo(userId);
//				}else {
//					if(stream != null && stream == 1){//收入
//						criteria.andOtherIdEqualTo(userId);
//					}else if(stream != null && stream == 0){
//						criteria.andUserIdEqualTo(userId);
//					}else{
//						criteria.andUserIdEqualTo(userId);
//						criteria2.andOtherIdEqualTo(userId);
//						example.or(criteria2);
//					}
//				}
				example.setOrderByClause("order_time desc");
				orderList = orderInfoMapper.selectByExample(example);
			}
			
			JSONArray array = new JSONArray();
			SellerInfo sellerInfo = null;
			UserInfo userInfo = null;
			
			for (OrderInfo order : orderList) {
				obj = new JSONObject();
				obj.put("orderId", order.getId().toString());
				obj.put("payId", order.getPayStreamId());
				obj.put("orderCode", StringUtils.trimToEmpty(order.getOrderCode()));
				obj.put("orderTime", DateTimeUtils.formatDateTime2String(order.getOrderTime(), "yyyy-MM-dd HH:mm"));
				obj.put("orderType", order.getOrderType());
				obj.put("orderStatus", order.getOrderStatus());
				if(order.getOrderType() == 1 || order.getOrderType() == 3){
					sellerInfo = sellerInfoMapper.selectByPrimaryKey(order.getSellerInfoId());
					obj.put("imgSrc", StringUtils.trimToEmpty(sellerInfo.getSellerHead()));
					obj.put("title", sellerInfo.getSellerName());
					obj.put("sellerId", order.getSellerInfoId().toString());
					
					if(order.getOrderType() == 1){
						if(order.getUserId().equals(userId)){
							userInfo = userInfoMapper.selectByPrimaryKey(order.getOtherId());
						}else{
							userInfo = userInfoMapper.selectByPrimaryKey(order.getUserId());
						}
						obj.put("otherUserId", userInfo.getId());
						obj.put("userPhone", userInfo.getUserPhone());
						obj.put("otherNickName", userInfo.getNickname());
					}else{
						obj.put("otherUserId", "");
						obj.put("userPhone", "");
						obj.put("otherNickName", "");
					}
					
				}else{
					if(order.getUserId().equals(userId)){
						userInfo = userInfoMapper.selectByPrimaryKey(order.getOtherId());
					}else{
						userInfo = userInfoMapper.selectByPrimaryKey(order.getUserId());
					}
					obj.put("imgSrc", StringUtils.trimToEmpty(userInfo.getImageHead()));
					obj.put("title", userInfo.getNickname());
					obj.put("userPhone", userInfo.getUserPhone());
					obj.put("sellerId", "");
					obj.put("otherUserId", userInfo.getId());
					obj.put("otherNickName", userInfo.getNickname());
				}
				
				if(order.getOrderType() == 1){
					Integer taskId = order.getTaskInfoId();
					obj.put("taskId", taskId.toString());
				}else{
					obj.put("taskId", "");
				}
				
				if(order.getPayType() != null){
					obj.put("payType", order.getPayType().toString());
					obj.put("totalPay", String.valueOf(((double)order.getAllMoney())/100));
					obj.put("payMoney", String.valueOf(((double)order.getPayMoney())/100));
				}else{
					obj.put("payType", "");
					obj.put("totalPay", "");
					obj.put("payMoney", "");
				}
				
				array.add(obj);
			}
			
			infoMap.put("orderInfoList", array);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		}catch(Exception e){
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 订单详情查询
	 * @param orderId 订单编号
	 * @author zq
	 */
	@RequestMapping(value="/orderDetail")
	@ResponseBody
	public Object orderDetail(HttpServletRequest request,Long orderId){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
		infoMap.put("resultDesc", "成功");
		try {
			//获取订单信息
			HashMap<String, Object> orderMap = orderInfoService.selectByOrderId(orderId);
			infoMap.put("orderInfo", orderMap);
			//获取订单支付列表
			List<HashMap<String, Object>> orderList = orderInfoService.selectPayByOrderId(orderId);
			infoMap.put("orderList", orderList);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		
		return infoMap;
	}

	
	/**
	 * 店铺订单列表
	 * @param orderStatus 订单状态
	 * @return
	 */
	@RequestMapping(value="/getOrderInfoList")
	@ResponseBody
	public Object getOrderInfoList(HttpServletRequest request,Integer orderStatus,int pageNow,int pageSize){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setOrderStatus(orderStatus);
		try{
			Page<?> page = PageHelper.startPage(pageNow,pageSize);
			List<LinkedHashMap<String,Object>> orderInfoList = orderInfoService.getOrderInfoList(orderInfo);
			infoMap.put("orderInfoList", orderInfoList);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		}catch(Exception e){
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 订单账单明细查询
	 * @param sellerId 店铺id,pageNow 页码,pageSize 每页条数
	 * @author 
	 */
	@RequestMapping(value="/orderPayDetail")
	@ResponseBody
	public Object orderPayDetail(HttpServletRequest request,int sellerId,int pageNow,int pageSize){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
		infoMap.put("resultDesc", "成功");
		try {
			//获取订单支付列表
			Page<?> page = PageHelper.startPage(pageNow,pageSize);
			List<HashMap<String, Object>> payList = orderInfoService.selectPayDetailByOrderId(sellerId);
			infoMap.put("payList", payList);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		
		return infoMap;
	}
	
	
	/**
	 * 金币明细
	 * @param request
	 * @param userToken
	 * @param orderType
	 * @param stream
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/getMygoldList")
	public Object getMygoldList(HttpServletRequest request,
			@RequestParam(value="userId") String userToken,
			@RequestParam(value="orderType",required=false) Integer orderType,//1充值；2赠送；3消费,4转账
			@RequestParam(value="stream",required=false) Integer stream,//1收入；0支出
			@RequestParam(value="pageNow",required=false,defaultValue="1") Integer pageNow,
			@RequestParam(value="pageSize",required=false,defaultValue="15") Integer pageSize){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		JSONArray array = new JSONArray();
		try{
			String configPath = PeiniUtils.IMGHOST;
			DecimalFormat    df   = new DecimalFormat("######0.00");   
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			GoldHistoryExample example = new GoldHistoryExample();
			
			com.jsz.peini.bean.order.GoldHistoryExample.Criteria criteria = example.createCriteria();
			com.jsz.peini.bean.order.GoldHistoryExample.Criteria criteria2= example.createCriteria();
			if(orderType!=null&&orderType!=0)
			{
				if(orderType==1)
				{
				  criteria.andHisTypeEqualTo(9);
				}
				else if(orderType==2)
				{
					criteria.andHisTypeEqualTo(1);
					criteria2.andHisTypeEqualTo(11);
					example.or(criteria2);
				}
				else if(orderType==3)
				{
					criteria.andHisTypeBetween(2,4);
					criteria2.andHisTypeEqualTo(10);
					example.or(criteria2);
				}
				else
				{
					criteria.andHisTypeEqualTo(7);
					criteria2.andHisTypeEqualTo(8);
					example.or(criteria2);
				}
			}
			if(stream!=null)
			{
				criteria.andOpTypeEqualTo(stream);
				criteria2.andOpTypeEqualTo(stream);
			}
			criteria.andUserIdEqualTo(userId);
			criteria2.andUserIdEqualTo(userId);
			example.setOrderByClause(" create_time desc");
			Page<?> page = PageHelper.startPage(pageNow,pageSize);
			List<GoldHistory> goldhist=goldHistoryMapper.selectByExample(example);
			SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
			Calendar calendar = Calendar.getInstance();
			for (GoldHistory gold : goldhist) {
				 obj = new JSONObject();
				 obj.put("hisid",gold.getHisId());
				 obj.put("hisType",gold.getHisType());
				 obj.put("userId",gold.getUserId());
				 obj.put("paramId",gold.getParamId());
				 if(gold.getValNum()!=null)
				 {
					 if(gold.getOpType()==1)
					 {
						 obj.put("valNum", "+"+df.format(gold.getValNum().doubleValue()/100));
					 }
					 else
					 {
						 obj.put("valNum", "-"+df.format(gold.getValNum().doubleValue()/100));
					 }
				 }
				 else
				 {
					 obj.put("valNum", 0);
				 }
				 calendar.setTime(gold.getCreateTime());
				 int year = calendar.get(Calendar.YEAR);//获取年份
			     int month=calendar.get(Calendar.MONTH)+1;//获取月份
			     int day=calendar.get(Calendar.DATE);//获取日
			     int hour=calendar.get(Calendar.HOUR_OF_DAY);//小时
			     int minute=calendar.get(Calendar.MINUTE);//分
			     String months=String.valueOf(month);
			     String days=String.valueOf(day);
			     String hours=String.valueOf(hour);
			     String minutes=String.valueOf(minute);
			     if(month>0&&month<10)
			     {
			    	 months="0"+month;
			     }
			     if(day>0&&day<10)
			     {
			    	 days="0"+day;
			     }
			     if(hour>0&&hour<10)
			     {
			    	 hours="0"+hour;
			     }
			     if(minute>0&&minute<10)
			     {
			    	 minutes="0"+minute;
			     }
			     obj.put("YTD", sdf.format(gold.getCreateTime()));
				 obj.put("time",hours+":"+minutes);
				 if(gold.getHisType()==1)
				 {
					 obj.put("image", "images/g-l-gw.png");
					 obj.put("typedes","官方");
					 obj.put("describe", "-活动赠送");
				 }
				 else if(gold.getHisType()==2)
				 {
					 TaskInfo taskinfo=taskInfoMapper.selectByPrimaryKey(Integer.valueOf(gold.getParamId()));
					 SellerInfo seelerinfo=sellerInfoMapper.selectByPrimaryKey(taskinfo.getSellerInfoId());
					 obj.put("image", configPath+seelerinfo.getSellerHead());
					 if(taskinfo!=null)
					 {
						 obj.put("typedes",   taskinfo.getSellerInfoName());
						 obj.put("describe","-任务消费");
					 }
					 else
					 {
						 obj.put("typedes",  "");
						 obj.put("describe","");
					 }
					
				 }
				 else if(gold.getHisType()==3)
				 {
					 obj.put("image", "images/g-l-gw.png");
					 obj.put("typedes",  "官方");
					 obj.put("describe", "-补签消费");
				 }
				 else if(gold.getHisType()==4)
				 {
					 OrderInfo orderinfo=orderInfoMapper.selectByPrimaryKey(Long.valueOf(gold.getParamId()));
					 SellerInfo info=sellerInfoMapper.selectByPrimaryKey(orderinfo.getSellerInfoId());
					 obj.put("image", configPath+info.getSellerHead());
					 obj.put("typedes",  info.getSellerName());
					 obj.put("describe", "-买单消费");
				 }
				 else if(gold.getHisType()==7)
				 {
					 UserInfo userinfo=userInfoMapper.selectByPrimaryKey(gold.getOtherid());
					 if(userinfo.getImageHead()==null)
					 {
						 if(userinfo.getSex()==1)
						 {
							 obj.put("image", "images/ic_nan.png");
						 }
						 else
						 {
							 obj.put("image", "images/ic_nv.png");
						 }
					 }
					 else
					 {
						 obj.put("image", configPath+userinfo.getImageHead());
					 }
					 if(userinfo!=null)
					 {
						 obj.put("typedes",  userinfo.getNickname());
						 obj.put("describe","-金币转账");
					 }
					 else
					 {
						 obj.put("typedes", "");
						 obj.put("describe","");
					 }
					 
				 }
				 else if(gold.getHisType()==8)
				 {
					 UserInfo userinfo=userInfoMapper.selectByPrimaryKey(gold.getOtherid());
					 if(userinfo.getImageHead()==null)
					 {
						 if(userinfo.getSex()==1)
						 {
							 obj.put("image", "images/ic_nan.png");
						 }
						 else
						 {
							 obj.put("image", "images/ic_nv.png");
						 }
					 }
					 else
					 {
						 obj.put("image", configPath+userinfo.getImageHead());
					 }
					 obj.put("image", configPath+userinfo.getImageHead());
					 if(userinfo!=null)
					 {
						 obj.put("typedes", userinfo.getNickname());
						 obj.put("describe", "-金币转账");
					 }
					 else
					 {
						 obj.put("typedes", "");
						 obj.put("describe", "");
					 }
					
				 }
				 else if(gold.getHisType()==9)
				 {
					 OrderInfo orderinfo=orderInfoMapper.selectByPrimaryKey(Long.valueOf(gold.getParamId()));
//					 OrderInfoExample example1=new OrderInfoExample();
//					 Criteria criteria3=example1.createCriteria();
//					 criteria3.andParamIdEqualTo(gold.getParamId());
//					 List<OrderInfo> orderinfoList=orderInfoMapper.selectByExample(example1);
					if(orderinfo!=null)
					{
//						OrderInfo orderinfo=orderinfoList.get(0);
						if(orderinfo.getPayType()==3)
						{
							obj.put("image", "images/zhifubao.png");
							 obj.put("typedes","支付宝");
							obj.put("describe", "-金币充值");
						}
						else if(orderinfo.getPayType()==2)
						{
							obj.put("image", "images/g-l-wei.png");
							 obj.put("typedes","微信");
							obj.put("describe", "-金币充值");
						}
						 
					}
					else
					{
						obj.put("typedes", "");
						obj.put("describe", "");
					}
					 
				 }
				 else if(gold.getHisType()==10)
				 {
					 ActivityInfo activityInfo=activityInfoMapper.selectByPrimaryKey(gold.getParamId());
					 obj.put("image", "images/jinbi.png");
					 if(activityInfo!=null)
					 {
						 obj.put("typedes", activityInfo.getAcTitle());
						 obj.put("describe", "-活动消费");
					 }
					 else
					 {
						 obj.put("typedes", "");
						 obj.put("describe","");
					 }
				 }
				 else
				 {
					 obj.put("image", "images/jinbi.png");
					 obj.put("typedes", "官方");
					 obj.put("describe", "-活动赠送");
				 }
				
				array.add(obj);
			}
			request.setAttribute("userToken", userToken);
			request.setAttribute("orderType", orderType);
			request.setAttribute("stream", stream);
			request.setAttribute("goldHistoryList", array);
//			infoMap.put("goldHistoryList", array);
//			infoMap.put("resultCode", Constants.SUCCESS);
//			infoMap.put("resultDesc", "成功");
		}catch(Exception e){
			request.setAttribute("goldHistoryList", array);
			request.setAttribute("userToken", "");
			request.setAttribute("orderType", "");
			request.setAttribute("stream", "");
//			infoMap.put("resultCode", Constants.FAIL);
//			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return "detail/goldDetail";
	}
	
	/**
	 * 
	 * @param request
	 * @param userToken
	 * @param orderType
	 * @param stream
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getMygoldListJson")
	public Object getMygoldListJson(HttpServletRequest request,
			@RequestParam(value="userId") String userToken,
			@RequestParam(value="orderType",required=false) Integer orderType,//1充值；2赠送；3消费,4转账
			@RequestParam(value="stream",required=false) Integer stream,//1收入；0支出
			@RequestParam(value="pageNow",required=false,defaultValue="1") Integer pageNow,
			@RequestParam(value="pageSize",required=false,defaultValue="15") Integer pageSize){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		JSONObject object=new JSONObject();
		JSONArray array = new JSONArray();
		try{
			DecimalFormat    df   = new DecimalFormat("######0.00");
			String configPath = PeiniUtils.IMGHOST;
			//JSONObject obj=new JSONObject();
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			//String userId= userToken;
			GoldHistoryExample example = new GoldHistoryExample();
			
			com.jsz.peini.bean.order.GoldHistoryExample.Criteria criteria = example.createCriteria();
			com.jsz.peini.bean.order.GoldHistoryExample.Criteria criteria2= example.createCriteria();
			if(orderType!=null&&orderType!=0)
			{
				if(orderType==1)
				{
				  criteria.andHisTypeEqualTo(9);
				}
				else if(orderType==2)
				{
					criteria.andHisTypeEqualTo(1);
					criteria2.andHisTypeEqualTo(11);
					example.or(criteria2);
				}
				else if(orderType==3)
				{
					criteria.andHisTypeBetween(2,4);
					criteria2.andHisTypeEqualTo(10);
					example.or(criteria2);
				}
				else
				{
					criteria.andHisTypeEqualTo(7);
					criteria2.andHisTypeEqualTo(8);
					example.or(criteria2);
				}
			}
			if(stream!=null)
			{
				criteria.andOpTypeEqualTo(stream);
				criteria2.andOpTypeEqualTo(stream);
			}
			criteria.andUserIdEqualTo(userId);
			criteria2.andUserIdEqualTo(userId);
			example.setOrderByClause(" create_time desc");
			Page<?> page = PageHelper.startPage(pageNow,pageSize);
			List<GoldHistory> goldhist=goldHistoryMapper.selectByExample(example);
			SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
			Calendar calendar = Calendar.getInstance();
			for (GoldHistory gold : goldhist) {
				 obj = new JSONObject();
				 obj.put("hisid",gold.getHisId());
				 obj.put("hisType",gold.getHisType());
				 obj.put("userId",gold.getUserId());
				 obj.put("paramId",gold.getParamId());
				 if(gold.getValNum()!=null)
				 {
					 if(gold.getOpType()==1)
					 {
						 obj.put("valNum", "+"+df.format(gold.getValNum().doubleValue()/100));
					 }
					 else
					 {
						 obj.put("valNum", "-"+df.format(gold.getValNum().doubleValue()/100));
					 }
				 }
				 else
				 {
					obj.put("valNum",0);
				 }
				
				 calendar.setTime(gold.getCreateTime());
				 int year = calendar.get(Calendar.YEAR);//获取年份
			     int month=calendar.get(Calendar.MONTH);//获取月份
			     int day=calendar.get(Calendar.DATE);//获取日
			     int hour=calendar.get(Calendar.HOUR_OF_DAY);//小时
			     int minute=calendar.get(Calendar.MINUTE);//分
			     String months=String.valueOf(month);
			     String days=String.valueOf(day);
			     String hours=String.valueOf(hour);
			     String minutes=String.valueOf(minute);
			     if(month>0&&month<10)
			     {
			    	 months="0"+month;
			     }
			     if(day>0&&day<10)
			     {
			    	 days="0"+day;
			     }
			     if(hour>0&&hour<10)
			     {
			    	 hours="0"+hour;
			     }
			     if(minute>0&&minute<10)
			     {
			    	 minutes="0"+minute;
			     }
			     obj.put("YTD", sdf.format(gold.getCreateTime()));
				 obj.put("time",hours+":"+minutes);
				 if(gold.getHisType()==1)
				 {
					 obj.put("image", "images/g-l-gw.png");
					 obj.put("typedes", "官方");
					 obj.put("describe", "-活动赠送");
				 }
				 else if(gold.getHisType()==2)
				 {
					 TaskInfo taskinfo=taskInfoMapper.selectByPrimaryKey(Integer.valueOf(gold.getParamId()));
					 SellerInfo seelerinfo=sellerInfoMapper.selectByPrimaryKey(taskinfo.getSellerInfoId());
					 obj.put("image", configPath+seelerinfo.getSellerHead());
					 if(taskinfo!=null)
					 {
						 obj.put("typedes", taskinfo.getSellerInfoName());
						 obj.put("describe", "-任务消费");
					 }
					 else
					 {
						 obj.put("typedes","");
						 obj.put("describe","");
					 }
					
				 }
				 else if(gold.getHisType()==3)
				 {
					 obj.put("image", "images/g-l-gw.png");
					 obj.put("typedes", "官方");
					 obj.put("describe", "-补签消费");
				 }
				 else if(gold.getHisType()==4)
				 {
					 OrderInfo orderinfo=orderInfoMapper.selectByPrimaryKey(Long.valueOf(gold.getParamId()));
					 SellerInfo info=sellerInfoMapper.selectByPrimaryKey(orderinfo.getSellerInfoId());
					 obj.put("image", configPath+info.getSellerHead());
					 obj.put("typedes", info.getSellerName());
					 obj.put("describe","-买单消费");
				 }
				 else if(gold.getHisType()==7)
				 {
					 UserInfo userinfo=userInfoMapper.selectByPrimaryKey(gold.getOtherid());
					 if(userinfo.getImageHead()==null)
					 {
						 if(userinfo.getSex()==1)
						 {
							 obj.put("image", "images/ic_nan.png");
						 }
						 else
						 {
							 obj.put("image", "images/ic_nv.png");
						 }
					 }
					 else
					 {
						 obj.put("image", configPath+userinfo.getImageHead());
					 }
					 
					 if(userinfo!=null)
					 {
						 obj.put("typedes", userinfo.getNickname());
						 obj.put("describe", "-金币转账");
					 }
					 else
					 {
						 obj.put("typedes","");
						 obj.put("describe","");
					 }
					 
				 }
				 else if(gold.getHisType()==8)
				 {
					 UserInfo userinfo=userInfoMapper.selectByPrimaryKey(gold.getOtherid());
					 if(userinfo.getImageHead()==null)
					 {
						 if(userinfo.getSex()==1)
						 {
							 obj.put("image", "images/ic_nan.png");
						 }
						 else
						 {
							 obj.put("image", "images/ic_nv.png");
						 }
					 }
					 else
					 {
						 obj.put("image", configPath+userinfo.getImageHead());
					 }
					 if(userinfo!=null)
					 {
						 obj.put("typedes",userinfo.getNickname());
						 obj.put("describe","-金币转账");
					 }
					 else
					 {
						 obj.put("typedes","");
						 obj.put("describe", "");
					 }
					
				 }
				 else if(gold.getHisType()==9)
				 {
					 OrderInfo orderinfo=orderInfoMapper.selectByPrimaryKey(Long.valueOf(gold.getParamId()));
//					 OrderInfoExample example1=new OrderInfoExample();
//					 Criteria criteria3=example1.createCriteria();
//					 criteria3.andParamIdEqualTo(gold.getParamId());
//					 List<OrderInfo> orderinfoList=orderInfoMapper.selectByExample(example1);
					if(orderinfo!=null)
					{
//						OrderInfo orderinfo=orderinfoList.get(0);
						if(orderinfo.getPayType()==3)
						{
							obj.put("image", "images/zhifubao.png");
							obj.put("typedes","支付宝");
							obj.put("describe", "-金币充值");
						}
						else if(orderinfo.getPayType()==2)
						{
							obj.put("image", "images/g-l-wei.png");
							obj.put("typedes","微信");
							obj.put("describe", "-金币充值");
						}
						 
					}
					else
					{
						obj.put("typedes","");
						obj.put("describe", "");
					}
					 
				 }
				 else if(gold.getHisType()==10)
				 {
					 ActivityInfo activityInfo=activityInfoMapper.selectByPrimaryKey(gold.getParamId());
					 obj.put("image", "images/jinbi.png");
					 if(activityInfo!=null)
					 {
						 obj.put("typedes",activityInfo.getAcTitle());
						 obj.put("describe", "-活动消费");
					 }
					 else
					 {
						 obj.put("typedes","");
						 obj.put("describe","");
					 }
				 }
				 else
				 {
					 obj.put("image", "images/jinbi.png");
					 obj.put("typedes","官方");
					 obj.put("describe", "-活动赠送");
				 }
				
				array.add(obj);
			}
			object.put("goldHistoryList", array);
//			infoMap.put("goldHistoryList", array);
//			infoMap.put("resultCode", Constants.SUCCESS);
//			infoMap.put("resultDesc", "成功");
		}catch(Exception e){
			object.put("goldHistoryList","");
//			infoMap.put("resultCode", Constants.FAIL);
//			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return object.toString();
	}
	
	/**
	 * 支付成功页面
	 * @author Han Guohong
	 * @param request
	 * @param userId
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/wap/pay-details")
	public Object orderDetail(HttpServletRequest request,
			@RequestParam(value="userId") String token,
			@RequestParam(value="orderId") String orderId
			){
		
		try {
			
			OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(Long.parseLong(orderId));
			
			if(orderInfo != null){
				request.setAttribute("orderInfo", orderInfo);
				
				if(orderInfo.getOrderType() == 1){
					OrderSellerExample example = new OrderSellerExample();
					example.createCriteria().andOrderTypeEqualTo(3).andTaskIdEqualTo((long)orderInfo.getTaskInfoId());
					List<OrderSeller> list = orderSellerMapper.selectByExample(example);
					
					if(list.size() > 0){
						request.setAttribute("orderSeller", list.get(0));
					}
					
					Integer sellerId = orderInfo.getSellerInfoId();
					SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId);
					if(StringUtils.isNotBlank(sellerInfo.getSellerHead())){
						sellerInfo.setSellerHead(PeiniUtils.IMGHOST+sellerInfo.getSellerHead());
					}
					
					request.setAttribute("sellerInfo", sellerInfo);
				}else if(orderInfo.getOrderType() == 3){
					OrderSellerExample example = new OrderSellerExample();
					example.createCriteria().andOrderTypeEqualTo(3).andTaskIdEqualTo(Long.parseLong(orderId));
					List<OrderSeller> list = orderSellerMapper.selectByExample(example);
					
					if(list.size() > 0){
						request.setAttribute("orderSeller", list.get(0));
					}
					
					Integer sellerId = orderInfo.getSellerInfoId();
					SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId);
					if(StringUtils.isNotBlank(sellerInfo.getSellerHead())){
						sellerInfo.setSellerHead(PeiniUtils.IMGHOST+sellerInfo.getSellerHead());
					}
					
					request.setAttribute("sellerInfo", sellerInfo);
				}else if(orderInfo.getOrderType() == 5) {
					UserInfo userInfo = userInfoMapper.selectByPrimaryKey(orderInfo.getOtherId());
					userInfo.setNickname(PeiniUtils.updateNickName(userInfo.getNickname(), userInfo.getAccCode()));
					if(StringUtils.isBlank(userInfo.getImageHead())){
						userInfo.setImageHead("images/ic_nan.png");
					}else{
						userInfo.setImageHead(PeiniUtils.IMGHOST+userInfo.getImageHead());
					}
					request.setAttribute("userInfo", userInfo);
				}
				
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		request.setAttribute("userId",token);
		return "order/pay-details";
	}
	
	@RequestMapping(value="/wap/paySeller-details")
	public Object orderSellerDetail(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="orderId") String orderId
			){
		
		try {
			
			OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(Long.parseLong(orderId));
			
			if(orderInfo != null){
				request.setAttribute("orderInfo", orderInfo);
				
				if(orderInfo.getOrderType() == 1){
					OrderSellerExample example = new OrderSellerExample();
					example.createCriteria().andOrderTypeEqualTo(3).andTaskIdEqualTo((long)orderInfo.getTaskInfoId());
					List<OrderSeller> list = orderSellerMapper.selectByExample(example);
					
					if(list.size() > 0){
						request.setAttribute("orderSeller", list.get(0));
					}
				}else if(orderInfo.getOrderType() == 3){
					OrderSellerExample example = new OrderSellerExample();
					example.createCriteria().andOrderTypeEqualTo(3).andTaskIdEqualTo(Long.parseLong(orderId));
					List<OrderSeller> list = orderSellerMapper.selectByExample(example);
					
					if(list.size() > 0){
						request.setAttribute("orderSeller", list.get(0));
					}
				}else if(orderInfo.getOrderType() == 6) {
					
				}
				
				Integer sellerId = orderInfo.getSellerInfoId();
				SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId);
				if(StringUtils.isNotBlank(sellerInfo.getSellerHead())){
					sellerInfo.setSellerHead(PeiniUtils.IMGHOST+sellerInfo.getSellerHead());
				}
				
				request.setAttribute("sellerInfo", sellerInfo);
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		request.setAttribute("userId",UserRedisUtil.getUserToken(userId));
		return "order/pay-details";
	}
	
	@RequestMapping(value="/wap/payGold-details")
	public Object orderGoldDetail(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="orderId") String orderId
			){
		
		try {
			
			OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(Long.parseLong(orderId));
			
			if(orderInfo != null){
				request.setAttribute("orderInfo", orderInfo);
				
				if(orderInfo.getOrderType() == 5){
					UserInfo userInfo = userInfoMapper.selectByPrimaryKey(orderInfo.getOtherId());
					userInfo.setNickname(PeiniUtils.updateNickName(userInfo.getNickname(), userInfo.getAccCode()));
					if(StringUtils.isBlank(userInfo.getImageHead())){
						userInfo.setImageHead("images/ic_nan.png");
					}else{
						userInfo.setImageHead(PeiniUtils.IMGHOST+userInfo.getImageHead());
					}
					request.setAttribute("userInfo", userInfo);
				}else if(orderInfo.getOrderType() == 6){
					UserInfo userInfo = userInfoMapper.selectByPrimaryKey(orderInfo.getUserId());
					userInfo.setNickname(PeiniUtils.updateNickName(userInfo.getNickname(), userInfo.getAccCode()));
					if(StringUtils.isBlank(userInfo.getImageHead())){
						userInfo.setImageHead("images/ic_nan.png");
					}else{
						userInfo.setImageHead(PeiniUtils.IMGHOST+userInfo.getImageHead());
					}
					request.setAttribute("userInfo", userInfo);
				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		request.setAttribute("userId",UserRedisUtil.getUserToken(userId));
		return "order/pay-details";
	}
	
	@RequestMapping(value="/wap/taskPay-details")
	public Object taskPayDetails(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="taskId") String taskId
			){
		
		try {
			
			TaskInfo taskInfo = taskInfoMapper.selectByPrimaryKey(Integer.parseInt(taskId));
			request.setAttribute("taskInfo", taskInfo);
			
			UserInfo otherInfo = null;
			
			Long orderId = 0l;
			if(taskInfo.getUserId().equals(userId)){
				orderId = taskInfo.getOrderId();
				otherInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getOtherUserId());
			}else{
				orderId = taskInfo.getOtherOrderId();
				otherInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getUserId());
			}
			OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
			request.setAttribute("otherInfo", otherInfo);
			
			if(orderInfo != null){
				request.setAttribute("orderInfo", orderInfo);
				
				if(orderInfo.getOrderType() == 1){
					OrderSellerExample example = new OrderSellerExample();
					example.createCriteria().andOrderTypeEqualTo(3).andTaskIdEqualTo((long)orderInfo.getTaskInfoId());
					List<OrderSeller> list = orderSellerMapper.selectByExample(example);
					
					if(list.size() > 0){
						request.setAttribute("orderSeller", list.get(0));
					}
				}else if(orderInfo.getOrderType() == 3){
					OrderSellerExample example = new OrderSellerExample();
					example.createCriteria().andOrderTypeEqualTo(3).andTaskIdEqualTo(orderId);
					List<OrderSeller> list = orderSellerMapper.selectByExample(example);
					
					if(list.size() > 0){
						request.setAttribute("orderSeller", list.get(0));
					}
				}else if(orderInfo.getOrderType() == 6) {
					
				}
				
				Integer sellerId = orderInfo.getSellerInfoId();
				SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId);
				if(StringUtils.isNotBlank(sellerInfo.getSellerHead())){
					sellerInfo.setSellerHead(PeiniUtils.IMGHOST+sellerInfo.getSellerHead());
				}
				
				request.setAttribute("sellerInfo", sellerInfo);
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		request.setAttribute("userId",UserRedisUtil.getUserToken(userId));
		return "order/pay-details";
	}
}
